package com.efast.dmom.mes.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.util.StringUtil;
import com.efast.dmom.mes.bean.PartInventory;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.datahandlers.entity.PartInventoryDataHandler;
import com.efast.dmom.mes.service.PartInventoryService;

@Service
public class PartInventoryServiceImpl implements PartInventoryService {

	@Autowired
	private PartInventoryDataHandler partInventoryDataHandler;

	@Override
	public ResultBean save(PartInventory partInventory) {
		ResultBean resultBean = new ResultBean(true, ' ');
		if (partInventory.getInnerData().getPartId() == 0 || partInventory.getInnerData().getQty() == 0
				|| !StringUtil.isNotBlank(partInventory.getInnerData().getName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查库存名称、物料、数量是否填写!");
			return resultBean;
		}
		PartInventory partInventoryByName = partInventoryDataHandler.getByName(partInventory.getInnerData().getName());
		if (partInventoryByName.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("库存名称已存在!");
			return resultBean;
		}
		PartInventory partInventoryByPartId = partInventoryDataHandler
				.getPartInventoryByPartId(partInventory.getInnerData().getPartId());
		if (partInventoryByPartId.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该物料在库存中已存在，只能编辑，不可修改!");
			return resultBean;
		}
		partInventoryDataHandler.save(partInventory);
		resultBean.setSuccess(true);
		resultBean.setMessage("库存保存成功!");
		return resultBean;
	}

	@Override
	public ResultBean update(PartInventory partInventory) {
		ResultBean resultBean = new ResultBean(true, ' ');
		if (partInventory.getInnerData().getPartId() == 0 || partInventory.getInnerData().getQty() == 0
				|| !StringUtil.isNotBlank(partInventory.getInnerData().getName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查库存名称、物料、数量是否填写!");
			return resultBean;
		}
		PartInventory partInventoryById = partInventoryDataHandler.getById(partInventory.getInnerData().getId());
		if (partInventoryById.getInnerData() == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("修改的物料库存不存在!");
			return resultBean;
		}
		if (!partInventoryById.getInnerData().getName().equals(partInventory.getInnerData().getName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("库存名称不可修改!");
			return resultBean;
		}
		if (partInventoryById.getInnerData().getPartId() != partInventory.getInnerData().getPartId()) {
			PartInventory partInventoryByPartId = partInventoryDataHandler
					.getPartInventoryByPartId(partInventory.getInnerData().getPartId());
			if (partInventoryByPartId.getInnerData() != null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("修改后的物料在库存中已存在,不可进行修改!");
				return resultBean;
			}
		}
		partInventoryDataHandler.update(partInventory);
		resultBean.setSuccess(true);
		resultBean.setMessage("库存修改成功!");
		return resultBean;
	}

	@Override
	public ResultBean delete(PartInventory partInventory) {
		ResultBean resultBean = new ResultBean(true, ' ');
		partInventoryDataHandler.delete(partInventory);
		resultBean.setSuccess(true);
		resultBean.setMessage("库存删除成功!");
		return resultBean;
	}

}
